#pragma once
#include"Log.hpp"
#include"Common.hpp"

#include<functional>
#include<unordered_map>

#define DEFAULT_FD -1
class Connection
{
public:
    using read_callback = std::function<void()>;
    using write_callback = std::function<void()>;
    using error_callback = std::function<void()>;
    Connection(int fd=DEFAULT_FD):
    _fd(fd),_isconnection(true){}
    
    void Set_efd(int fd)
    {
        _fd = fd;
        _isconnection = false;
    }

    int Fd()
    {
        return _fd;
    }
    
    void Set_read_callback(const read_callback& cb)
    {
        _readback = cb;
    }
    void Set_write_callback(const write_callback& wb)
    {
        _writeback = wb;
    }
    void Set_error_callback(const error_callback& eb)
    {
        _errorback = eb;
    }

    void Handle_read()
    {
        if(_isconnection)
        {
            _readback();
        }
    }
    void Handle_write()
    {
        if(_isconnection)
        {
            _writeback();
        }
    }
    void Handle_error()
    {
        if(_isconnection)
        {
            _errorback();
        }
    }
    bool Is_connection()
    {
        return _isconnection;
    }

private:
    int _fd;
    bool _isconnection;
    read_callback _readback;
    write_callback _writeback;
    error_callback _errorback;
   
};